package com.monkey.luffer.design.pattern.strategy;

import java.util.Comparator;

/**
 * @Author dengkaibing
 * @Date 2022/9/20 17:08
 * @Desc wrapperComparator 比较器
 */
public class OrderComparator implements Comparator<Object> {

    public static final Comparator<Object> COMPARATOR = new OrderComparator();

    /**
     * 重写比较方法  根据order从小到大排序
     * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return
     */
    @Override
    public int compare(Object o1, Object o2) {
        if (o1 == null && o2 == null) {
            return 0;
        }
        if (o1 == null) {
            return -1;
        }
        if (o2 == null) {
            return 1;
        }
        if (o1.equals(o2)) {
            return 0;
        }
        Order a1 = o1.getClass().getAnnotation(Order.class);

        Order a2 = o2.getClass().getAnnotation(Order.class);
        int n1 = a1 == null ? 0 : a1.order();
        int n2 = a2 == null ? 0 : a2.order();
        return n1 > n2 ? 1 : -1;
    }
}
